package com.atguigu.url.service.impl;

import com.atguigu.url.service.DouYinVideoDownService;
import com.atguigu.url.util.DownloadUtil;
import com.atguigu.url.util.JsonReadFromLocalFile;
import com.atguigu.url.util.StatisticsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/16 12:58
 * @description:
 */
@Service
public class DouYinVideoDownServiceImpl implements DouYinVideoDownService {

    @Value("${douyin.download.googlelogs}")
    private String googlelogs;
    @Value("${douyin.download.dstpath}")
    private String dstPath;
    @Value("${douyin.download.piece}")
    private int piece;

    @Autowired
    JsonReadFromLocalFile jsonRead;
    @Autowired
    StatisticsUtil statisticsUtil;
    @Autowired
    DownloadUtil downloadUtil;


    @Override
    public String executorDouYinVideoDown(Integer tag) {
        System.out.println(" 执行 ==> executorDouYinVideoDown(Integer tag) ");
        String str = "executorDouYinVideoDown->";

        if (tag == 10010) {
            System.out.println("service 10010");
            this.getDouYinVideo(dstPath, googlelogs);
            str += "10010ok->";
        }

        return str;
    }


    @Override
    public long downloadAllTime() {
        System.out.println(" 执行 ==> downloadAllTime() ");
        Set<String> urlSet = jsonRead.getUrlSetFromJsonByLine(googlelogs);
        long allTime = statisticsUtil.statistics1BatchUrlDownloadAllTime(urlSet);
        return allTime;
    }


    /**
     * 高效下载
     *
     * @param tag
     * @return
     */
    @Override
    public String getDouYinVideoFast(Integer tag) {
        System.out.println(" 执行 ==>  getDouYinVideoFast(Integer tag) ");

        Set<String> urlSet = jsonRead.getUrlSetFromJsonByLine(googlelogs);
        downloadUtil.downloadVideo(dstPath, urlSet, piece);

        return " 执行 ==> doDownloadVideo(dstPath, urlSet) ";
    }


    /**
     * 效率很低的下载方式
     *
     * @param localDstPath  a
     * @param jsonPath a
     */
    public void getDouYinVideo(String localDstPath, String jsonPath) {
        System.out.println(" 执行 ==> getDouYinVideo(String localDstPath, String jsonPath) ");

        HashSet<String> urlSet = jsonRead.getUrlSetByFullJson(jsonPath);
        downloadUtil.downloadVideo(localDstPath, urlSet, piece);
        //        statisticsDownVideoAllTime(urlSet);
    }

}

